from flask import Blueprint, request, session, redirect, render_template, url_for, g, jsonify

from apps.article.model import Article
from apps.user.model import Photo
from apps.utils.util import upload_qiniu
from ext import db

article_bp = Blueprint('article', __name__, url_prefix='/article')


@article_bp.route('/publish', methods=['POST', 'GET'])
def article_publish():
    if request.method == 'POST':
        uid = session.get('uid')
        title = request.form.get('title')
        article_type = request.form.get('article_type')
        content = request.form.get('content')
        article = Article()
        article.title = title
        article.user_id = uid
        article.type_id = article_type
        article.content = content
        db.session.add(article)
        db.session.commit()
        return redirect(url_for('user.index'))
    else:
        return redirect(url_for('user.user_show'))


@article_bp.route('/detail')
def article_detail():
    article_id = request.args.get('aid')
    article = Article.query.get(article_id)
    return render_template('articles/detail.html', user=g.user, article_types=g.article_types, article=article)


@article_bp.route('/type_select')
def article_type_select():
    page = request.args.get('page')
    if not page:
        page = 1
    article_type_id = request.args.get('tid')
    pagination = Article.query.filter(Article.type_id == int(article_type_id)).order_by(-Article.pdatetime).paginate(
        page=int(page), per_page=3)
    if g.user:
        return render_template('articles/type_list.html', user=g.user, article_types=g.article_types,
                               pagination=pagination)
    else:
        return render_template('articles/type_list.html', article_types=g.article_types, pagination=pagination)


@article_bp.route('love')
def article_love():
    aid = request.args.get('aid')
    article = Article.query.get(aid)
    article.love_num += 1
    db.session.commit()
    return jsonify(num=article.love_num)


@article_bp.route('delove')
def article_delove():
    aid = request.args.get('aid')
    article = Article.query.get(aid)
    article.love_num -= 1
    db.session.commit()
    return jsonify(num=article.love_num)


@article_bp.route('save')
def article_save():
    aid = request.args.get('aid')
    article = Article.query.get(aid)
    article.save_num += 1
    db.session.commit()
    return jsonify(num=article.save_num)


@article_bp.route('desave')
def article_desave():
    aid = request.args.get('aid')
    article = Article.query.get(aid)
    article.save_num -= 1
    db.session.commit()
    return jsonify(num=article.save_num)


@article_bp.route('/photo_upload', methods=['POST', 'GET'])
def photo_upload():
    uid = session.get('uid')
    photo = request.files.get('photo')
    info, key = upload_qiniu(photo)
    print('uid:' + str(uid))
    print('key:' + key)
    # print('info:' + info)
    photo1 = Photo()
    photo1.photo_name = key
    photo1.user_id = uid
    db.session.add(photo1)
    db.session.commit()
    return '上传成功'
